﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClsController;
using ClsUtilities;

namespace Pro_Care_System
{
    public partial class Administration : Form
    {
        private ClsLogicaNeg logica;

        public Administration()
        {
            InitializeComponent();
            logica = new ClsLogicaNeg();

        }

        private void Administration_Load(object sender, EventArgs e)
        {

            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.AGENCY,  TableStruct.AGENCY.AG_COD_AGENCY.ToString(), TableStruct.AGENCY.AG_NAME.ToString(), cbxagency);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.WORKERS, TableStruct.WORKERS.WRK_COD_WORKER.ToString (), TableStruct.WORKERS.WRK_NAME.ToString (), cbxcola);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.POSITION, TableStruct.POSITION.PST_COD_POSITON.ToString(), TableStruct.POSITION.PST_DESCR.ToString(), cbxoccupation);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.PATIENT, TableStruct.PATIENT.PA_COD_PATIENT.ToString(), TableStruct.PATIENT.PA_NAME.ToString(), cbxpatients);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.USERS, TableStruct.USERS.US_COD_USER.ToString(), TableStruct.USERS.US_USER_NAME.ToString(), cbxuser);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.TERAPISTA , TableStruct.WORKERS.WRK_COD_WORKER.ToString(), TableStruct.WORKERS.WRK_NAME.ToString(), cbxpt);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.TERAPISTA_ASSIST, TableStruct.WORKERS.WRK_COD_WORKER.ToString(), TableStruct.WORKERS.WRK_NAME.ToString(), cbxpta);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.AGENCY, TableStruct.AGENCY.AG_COD_AGENCY.ToString(), TableStruct.AGENCY.AG_NAME.ToString(), cbxagencypatients);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.ESTATUS, TableStruct.ESTATUS.CT_FIELD_CODE.ToString(), TableStruct.ESTATUS.CT_CODE_DESCR.ToString(), cbxestatus);
        
        }

        //Agencia

        private void cbxagency_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxagency.SelectedIndex >= 0)
            {
                logica.CargarAgencia(txtagency, txtphone, txtemail, txtaddress, cbxagency);
            }
        }

        private void btnnew_Click(object sender, EventArgs e)
        {
            Desbloquear_Campos_Agencia();
            Limpiar_Agencia();

        }

        private void Bloquear_Campos_Agencia()
        {
            cbxagency.Enabled = true;
            txtagency.ReadOnly = true;
            txtphone.ReadOnly = true;
            txtemail.ReadOnly = true;
            txtaddress.ReadOnly = true;
        }

        private void Desbloquear_Campos_Agencia()
        {
            cbxagency.Enabled = false;
            txtagency.ReadOnly = false;
            txtphone.ReadOnly = false;
            txtemail.ReadOnly = false;
            txtaddress.ReadOnly = false;
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            Desbloquear_Campos_Agencia();
        }

        private void Limpiar_Agencia()
        {
            txtagency.Text  = "";
            txtphone.Text = "";
            txtemail.Text = "";
            txtaddress.Text = "";
            cbxagency.DataSource = null;
            cbxagency.Items.Clear();

        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            if (cbxagency.SelectedIndex >= 0)
            {
                logica.ModificarAgencia(cbxagency, txtagency, txtphone, txtemail, txtaddress,20);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.AGENCY, TableStruct.AGENCY.AG_COD_AGENCY.ToString(), TableStruct.AGENCY.AG_NAME.ToString(), cbxagency);
                Bloquear_Campos_Agencia();
                MessageBox.Show("Agencia actualizada exitosamente");
            }
            else
            {
                logica.CrearAgencia(txtagency, txtphone, txtemail, txtaddress);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.AGENCY, TableStruct.AGENCY.AG_COD_AGENCY.ToString(), TableStruct.AGENCY.AG_NAME.ToString(), cbxagency);
                Bloquear_Campos_Agencia();
                MessageBox.Show("Agencia ingresada exitosamente");
            }
                
        }

        private void btnclean_Click(object sender, EventArgs e)
        {
            Limpiar_Agencia();
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            logica.ModificarAgencia(cbxagency, txtagency, txtphone, txtemail, txtaddress, 22);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.AGENCY, TableStruct.AGENCY.AG_COD_AGENCY.ToString(), TableStruct.AGENCY.AG_NAME.ToString(), cbxagency);
            Bloquear_Campos_Agencia();
            MessageBox.Show("Se actualizo el registro correctamente");
        }

        // COLABORADORES

        private void cbxcola_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxcola.SelectedIndex >= 0)
            {
                logica.CargarColaboradores(txtname, cbxoccupation, txtemailcola, txtaddresscola, txtphonecola, cbxcola);
            }
        }

        private void btnaddcola_Click(object sender, EventArgs e)
        {
            Desbloquear_Campos_Colaboradores();
            Limpiar_Colaboradores();
        }

        private void Bloquear_Campos_Colaboradores()
        {
            cbxcola.Enabled = true;
            txtname.ReadOnly = true;
            cbxoccupation.Enabled = false;
            txtemailcola.ReadOnly = true;
            txtaddresscola.ReadOnly = true;
            txtphonecola.ReadOnly = true;
        }

        private void Desbloquear_Campos_Colaboradores()
        {
            cbxcola.Enabled = false;
            txtname.ReadOnly = false;
            cbxoccupation.Enabled = true;
            txtemailcola.ReadOnly = false;
            txtaddresscola.ReadOnly = false;
            txtphonecola.ReadOnly = false;

        }

        private void Limpiar_Colaboradores()
        {
            txtname.Text = "";
            txtemailcola.Text  = "";
            txtaddresscola.Text ="";
            txtphonecola.Text  = "";

            cbxcola.Enabled = false;
            
            cbxcola.DataSource = null;
            cbxcola.Items.Clear();
        }

        private void btncleancola_Click(object sender, EventArgs e)
        {
            Limpiar_Colaboradores();
            cbxcola.Enabled = true;
            cbxoccupation.Enabled = false;
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.WORKERS, TableStruct.WORKERS.WRK_COD_WORKER.ToString (), TableStruct.WORKERS.WRK_NAME.ToString (), cbxcola);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.POSITION, TableStruct.POSITION.PST_COD_POSITON.ToString(), TableStruct.POSITION.PST_DESCR.ToString(), cbxoccupation);
            

        }

        private void button9_Click(object sender, EventArgs e)
        {
            Desbloquear_Campos_Colaboradores();
        }

        private void btnsavecola_Click(object sender, EventArgs e)
        {
            if (cbxcola.SelectedIndex >= 0)
            {
                logica.ModificarColaborador(cbxcola, txtname, cbxoccupation, txtemailcola, txtaddresscola, txtphonecola, 20);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.WORKERS, TableStruct.WORKERS.WRK_COD_WORKER.ToString (), TableStruct.WORKERS.WRK_NAME.ToString (), cbxcola );
                Bloquear_Campos_Colaboradores();
                MessageBox.Show("Agencia actualizada exitosamente");
            }
            else
            {
                logica.CrearColaborador(txtname, cbxoccupation, txtemailcola, txtaddresscola, txtphonecola);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.WORKERS, TableStruct.WORKERS.WRK_COD_WORKER.ToString(), TableStruct.WORKERS.WRK_NAME.ToString(), cbxcola);
                Bloquear_Campos_Colaboradores();
                MessageBox.Show("Colaborador ingresado exitosamente");
            }
        }

        private void btndeletecola_Click(object sender, EventArgs e)
        {
            logica.ModificarColaborador(cbxcola, txtname, cbxoccupation, txtemailcola, txtaddresscola, txtphonecola, 22);
            logica.CargarComboBoxCatalogos(TableStruct.TblInfo.WORKERS, TableStruct.WORKERS.WRK_COD_WORKER.ToString(), TableStruct.WORKERS.WRK_NAME.ToString(), cbxcola);
            Bloquear_Campos_Colaboradores();
            MessageBox.Show("Se actualizo el registro correctamente");
        }
        //PACIENTES

        private void cbxpatients_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxpatients.SelectedIndex >= 0)
            {
                logica.CargarPacientes(txtnom1, txtnom2, txtape1, txtape2, cbxagencypatients, cbxpt, cbxpta, dtpdate, txtcid, txtphonepati, txtaddresspa, cbxocupationpat, txtemailpa, cbxpatients);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Limpiar_Pacientes();
            desbloquear_Pacientes();
        }

        private void Limpiar_Pacientes()
        {
            txtnom1.Text = "";
            txtnom2.Text = "";
            txtape1.Text = "";
            txtape2.Text = "";
            txtcid.Text = "";
            txtphonepati.Text = "";
            txtaddresspa.Text = "";
            txtphonepati.Text = "";
            txtaddresspa.Text = "";
            txtemailpa.Text = "";

            cbxpatients.Enabled = false;
            cbxpatients.DataSource = null;
            cbxpatients.Items.Clear();
        }

        private void desbloquear_Pacientes()
        {
            txtnom1.ReadOnly =false;
            txtnom2.ReadOnly = false;
            txtape1.ReadOnly = false;
            txtape2.ReadOnly = false;
            txtcid.ReadOnly = false;
            txtphonepati.ReadOnly = false;
            txtaddresspa.ReadOnly = false;
            txtphonepati.ReadOnly = false;
            txtaddresspa.ReadOnly = false;
            txtemailpa.ReadOnly = false;

            cbxpatients.Enabled = false;
            cbxagencypatients.Enabled = true;
            cbxpt.Enabled = true;
            cbxpta.Enabled = true;
            dtpdate.Enabled = true;
            cbxocupationpat.Enabled = true;
        }

        private void bloquear_Pacientes()
        {
            txtnom1.ReadOnly = true;
            txtnom2.ReadOnly = true;
            txtape1.ReadOnly = true;
            txtape2.ReadOnly = true;
            txtcid.ReadOnly = true;
            txtphonepati.ReadOnly = true;
            txtaddresspa.ReadOnly = true;
            txtphonepati.ReadOnly = true;
            txtaddresspa.ReadOnly = true;
            txtemailpa.ReadOnly = true;

            cbxpatients.Enabled = true;
            cbxagencypatients.Enabled = false;
            cbxpt.Enabled = false;
            cbxpta.Enabled = false;
            dtpdate.Enabled = false;
            cbxocupationpat.Enabled = false;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            desbloquear_Pacientes();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (cbxpatients.SelectedIndex >= 0)
            {
                logica.ActualizaPacientes(cbxpatients,txtnom1,txtnom2,txtape1,txtape2,cbxagencypatients,dtpdate,cbxpt, cbxpta,txtphonepati,txtaddresspa,cbxocupationpat, txtemailpa,txtcid);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.PATIENT, TableStruct.PATIENT.PA_COD_PATIENT.ToString(), TableStruct.PATIENT.PA_NAME.ToString(), cbxpatients);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.TERAPISTA , TableStruct.WORKERS.WRK_COD_WORKER.ToString(), TableStruct.WORKERS.WRK_NAME.ToString(), cbxpt);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.TERAPISTA_ASSIST, TableStruct.WORKERS.WRK_COD_WORKER.ToString(), TableStruct.WORKERS.WRK_NAME.ToString(), cbxpta);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.AGENCY, TableStruct.AGENCY.AG_COD_AGENCY.ToString(), TableStruct.AGENCY.AG_NAME.ToString(), cbxagencypatients);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.POSITION, TableStruct.POSITION.PST_COD_POSITON.ToString(), TableStruct.POSITION.PST_DESCR.ToString(), cbxocupationpat);
                bloquear_Pacientes ();
                MessageBox.Show("Paciente actualizado exitosamente");
            }
            else
            {
                //txtnom1, txtnom2, txtape1, txtape2, cbxagencypatients, cbxpt, cbxpta, dtpdate, txtcid, txtphonepati, txtaddresspa, cbxocupationpat, txtemailpa, cbxpatients
                logica.CrearPacientes(txtnom1,txtnom2,txtape1,txtape2,cbxagencypatients,dtpdate,cbxpt, cbxpta,txtphonepati,txtaddresspa,cbxocupationpat, txtemailpa,txtcid );
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.PATIENT, TableStruct.PATIENT.PA_COD_PATIENT.ToString(), TableStruct.PATIENT.PA_NAME.ToString(), cbxpatients);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.TERAPISTA , TableStruct.WORKERS.WRK_COD_WORKER.ToString(), TableStruct.WORKERS.WRK_NAME.ToString(), cbxpt);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.TERAPISTA_ASSIST, TableStruct.WORKERS.WRK_COD_WORKER.ToString(), TableStruct.WORKERS.WRK_NAME.ToString(), cbxpta);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.AGENCY, TableStruct.AGENCY.AG_COD_AGENCY.ToString(), TableStruct.AGENCY.AG_NAME.ToString(), cbxagencypatients);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.POSITION, TableStruct.POSITION.PST_COD_POSITON.ToString(), TableStruct.POSITION.PST_DESCR.ToString(), cbxocupationpat);

                bloquear_Pacientes ();
                MessageBox.Show("Paciente ingresado exitosamente");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Limpiar_Pacientes();
        }

        private void cbxuser_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxuser.SelectedIndex >= 0)
            {
                logica.CargarUsers(txtusername, txtpass, cbxestatus, cbxuser);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Limpiar_Users();
            Desbloquear_Users();

        }
        public void Limpiar_Users()
        {
            txtusername.Text = "";
            txtpass.Text = "";

            cbxuser.Enabled = false;
            cbxuser.DataSource = null;
            cbxuser.Items.Clear();
        }
        public void Desbloquear_Users()
        {
            txtusername.ReadOnly =false;
            txtpass.ReadOnly = false;

            cbxestatus.Enabled = true;
        }
        public void Bloquear_Users()
        {
            txtusername.ReadOnly = true;
            txtpass.ReadOnly = true;

            cbxestatus.Enabled = false;
            cbxuser.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Desbloquear_Users();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (cbxuser.SelectedIndex >= 0)
            {
                logica.ModificarUsers(txtusername,txtpass,cbxestatus,cbxuser);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.ESTATUS, TableStruct.ESTATUS.CT_FIELD_CODE.ToString(), TableStruct.ESTATUS.CT_CODE_DESCR.ToString(), cbxestatus);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.USERS, TableStruct.USERS.US_COD_USER.ToString(), TableStruct.USERS.US_USER_NAME.ToString(), cbxuser);
                Bloquear_Users();
                MessageBox.Show("Usuario actualizado exitosamente");
            }
            else
            {
                logica.CrearUsers(txtusername, txtpass, cbxestatus);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.ESTATUS, TableStruct.ESTATUS.CT_FIELD_CODE.ToString(), TableStruct.ESTATUS.CT_CODE_DESCR.ToString(), cbxestatus);
                logica.CargarComboBoxCatalogos(TableStruct.TblInfo.USERS, TableStruct.USERS.US_COD_USER.ToString(), TableStruct.USERS.US_USER_NAME.ToString(), cbxuser);
                Bloquear_Users();
                MessageBox.Show("Usuario ingresado exitosamente");
            }
        }
    }
}
